<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use App\GoodStore;
use App\GoodStoreMember;
use Validator;
class GoodStoreMemberController extends Controller
{
    //
    public function create (Request $request) {
        $validator = Validator::make($request->all(), array(
            'name' => 'required',
            'memberId' => 'required',
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;


        $manager = DB::table('good_store_members')
            ->where('userId', $request->userId)
            ->first();
        if ($manager == null) {
            echo json_encode(['status' => 403, 'errmsg' => 'store not found', 'data' => []]);
            return ;
        }
        $member = DB::table('good_store_members')
            ->where('memberId', $request->memberId)
            ->first();
        if ($member != null) {
            echo json_encode(['status' => 403, 'errmsg' => 'this member has already created', 'data' => []]);
            return ;
        }


        $member = new GoodStoreMember();
        $member->userId = $request->userId;
        $member->name = $request->name;
        $member->memberId = $request->memberId;
        $member->storeId = $manager->storeId;
        $member->identity = $request->identity;
        if ($request->description)
            $member->description = $request->description;

        $member->save();

        echo json_encode(['status'=> 200, 'errmsg'=> '', 'data' => []]);

    }
    public function hasStore(Request $request)
    {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;


        $member = DB::table('good_store_members')
            ->where('userId', $request->userId)
            ->first();
        if ($member == null) {
            echo json_encode(['status' => 200, 'errmsg' => '', 'data' => 0]);
            return ;
        }
        $store = GoodStore::find($member->storeId);
        echo json_encode(['status' => 200, 'errmsg' => '', 'data' => [
            "storeId" => $store->id,
            "type" => $store->type,
            "identity" => $member->id
        ]]);
    }

    public function edit(Request $request) {
        $validator = Validator::make($request->all(), array(
            'memberId' => 'required',
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;


        $member = GoodStoreMember::where('memberId', $request->memberId)
            ->first();
        if ($member == null) {
            echo json_encode(['status' => 403, 'errmsg' => 'member not found', 'data' => []]);
            return ;
        }
        $member->name = $request->name;
        $member->identity  = $request->identity;
        $member->description = $request->description;
        $member->save();
        
        $this->returnEcho();
        return ;
    }

    public function findMembers(Request $request) {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;

        $member = DB::table('good_store_members')
            ->where('memberId', $request->userId)
            ->first();
        if ($member == null) {
            $this->returnEcho(403, 'store not found', '');
            return ;
        }

        $members = DB::table('good_store_members')
            ->where('storeId', $member->storeId)
            ->get();
        
        $this->returnEcho(200, '', $members);
    }

    public function deleteMembers(Request $request) {
        $validator = Validator::make($request->all(), array(
            'memberIds' => 'required',
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;

        $memberIds = json_decode($request->memberIds);
        foreach ($memberIds as $memberId) {
            $member = DB::table('good_store_members')
                ->where('memberId', $memberId);
            $member->delete();
        }

        $this->returnEcho();

    }


    public function getSomeMemberDetails(Request $request) {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            echo json_encode(['status' => 0, 'errmsg' => $validator->errors(), 'data' => []]);
            return ;
        }
        $token = new \App\AppToken();
        $result = $token->ValidateSession($request->sessionId);
        if ($result == false) {
            echo json_encode(['status' => 403, 'errmsg' => 'sessionId not found', 'data' => []]);
            return ;
        }
        $members =GoodStoreMember::where('storeId', $request->userId)->get();
        $data = [];
        foreach ($members as $member) {
            array_push($data, [
                "memberId" => $member->memberId,
                "name" => $member->name,
            ]);
        }
        echo json_encode(['status' => 200, 'errmsg' => '', 'data' =>['list'=>$data]]);
    }

}
